<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="如果在流中查找字符串，需要设置一个大小适中的缓存， static void Main(string[] args){HttpListener listener &#x3D; new HttpListener();listener.Prefixes.Add(“http:&#x2F;&#x2F;localhost:8081&#x2F;ListenerTest&#x2F;&quot;);listener.Start();   while (1&#x3D;&#x3D;1){">
<meta property="og:type" content="article">
<meta property="og:title" content="在流数据中查找特定字符串">
<meta property="og:url" content="http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/index.html">
<meta property="og:site_name" content="TmoonSite">
<meta property="og:description" content="如果在流中查找字符串，需要设置一个大小适中的缓存， static void Main(string[] args){HttpListener listener &#x3D; new HttpListener();listener.Prefixes.Add(“http:&#x2F;&#x2F;localhost:8081&#x2F;ListenerTest&#x2F;&quot;);listener.Start();   while (1&#x3D;&#x3D;1){">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2015-11-09T03:00:49.000Z">
<meta property="article:modified_time" content="2020-01-07T10:48:14.928Z">
<meta property="article:author" content="Tmoonlight">
<meta name="twitter:card" content="summary">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/logo2.gif" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/logo2.gif">
          
        
    
    <!-- title -->
    <title>在流数据中查找特定字符串</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2015/11/20/%E4%BB%8ESQL2008%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AF%BC%E5%87%BA%E6%95%B0%E6%8D%AE%E5%88%B0%E5%8F%A6%E5%A4%96%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E6%9C%89%E8%87%AA%E5%A2%9E%E4%B8%BB%E9%94%AE%EF%BC%8C%E9%82%A3%E4%B9%88%E5%9C%A8%E5%AF%BC%E5%87%BA%E7%9A%84%E6%97%B6%E5%80%99%E4%BC%9A%E6%8A%A5%E4%B8%80%E4%B8%AA%E9%94%99%E8%AF%AF/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2015/11/07/discuzapi%E6%94%B9%E9%80%A0/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&text=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&is_video=false&description=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=在流数据中查找特定字符串&body=Check out this article: http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&name=在流数据中查找特定字符串&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&t=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        在流数据中查找特定字符串
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">TmoonSite</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2015-11-09T03:00:49.000Z" itemprop="datePublished">2015-11-09</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <p>如果在流中查找字符串，需要设置一个大小适中的缓存，</p>
<p>static void Main(string[] args)<br>{<br>HttpListener listener = new HttpListener();<br>listener.Prefixes.Add(“<a href="http://localhost:8081/ListenerTest/&quot;" target="_blank" rel="noopener">http://localhost:8081/ListenerTest/&quot;</a>);<br>listener.Start();  </p>
<p>while (1==1)<br>{<br>HttpListenerContext context = listener.GetContext();<br>Task.Run(() =&gt;<br>{<br>if (context.Request.HttpMethod == “POST”)<br>{<br>SaveFile(context.Request.ContentEncoding, GetBoundary(context.Request.ContentType),<br>context.Request.InputStream);<br>}  </p>
<p>context.Response.StatusCode = 200;<br>context.Response.ContentType = “text/html”;<br>context.Response.AddHeader(“Access-Control-Allow-Origin”, “*”);<br>using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))<br>writer.WriteLine(“File Uploaded”);  </p>
<p>context.Response.Close();  </p>
<p>});<br>}<br>listener.Stop();  </p>
<p>}  </p>
<p>private static String GetBoundary(String ctype)<br>{<br>return “--“ + ctype.Split(‘;’)[1].Split(‘=’)[1];<br>}  </p>
<p>//之前没找到boundary是因为我没有调用getboundary，直接把contenttype穿过去了<br>private static string SaveFile(Encoding enc, String contentType, Stream input)<br>{  </p>
<p>Byte[] boundaryBytes = enc.GetBytes(GetBoundary(contentType));<br>Int32 boundaryLen = boundaryBytes.Length;<br>string fileName = Guid.NewGuid().ToString(“N”) + “.temp”;<br>using (FileStream output = new FileStream(fileName, FileMode.Create, FileAccess.Write))<br>{<br>Byte[] buffer = new Byte[1024];<br>Int32 len = input.Read(buffer, 0, 1024);<br>Int32 startPos = -1;  </p>
<p>// Find start boundary<br>while (true)<br>{<br>if (len == 0)<br>{<br>throw new Exception(“Start Boundaray Not Found”);<br>}  </p>
<p>startPos = IndexOf(buffer, len, boundaryBytes);<br>if (startPos &gt;= 0)<br>{<br>break;<br>}<br>else<br>{<br>Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);<br>len = input.Read(buffer, boundaryLen, 1024 - boundaryLen);<br>}<br>}  </p>
<p>// Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank)<br>for (Int32 i = 0; i &lt; 4; i++)<br>{<br>while (true)<br>{<br>if (len == 0)<br>{<br>throw new Exception(“Preamble not Found.”);<br>}  </p>
<p>startPos = Array.IndexOf(buffer, enc.GetBytes(“\n”)[0], startPos);<br>if (startPos &gt;= 0)<br>{<br>startPos++;<br>break;<br>}<br>else<br>{<br>len = input.Read(buffer, 0, 1024);<br>}<br>}<br>}  </p>
<p>Array.Copy(buffer, startPos, buffer, 0, len - startPos);<br>len = len - startPos;  </p>
<p>while (true)<br>{<br>Int32 endPos = IndexOf(buffer, len, boundaryBytes);<br>if (endPos &gt;= 0)<br>{<br>if (endPos &gt; 0) output.Write(buffer, 0, endPos - 2);<br>break;<br>}<br>else if (len &lt;= boundaryLen)<br>{<br>throw new Exception(“End Boundaray Not Found”);<br>}<br>else<br>{<br>output.Write(buffer, 0, len - boundaryLen);<br>//每次放置后40个字节到首部，读取接下来984个字节，在此基础上进行byte查找，绝妙！<br>Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);<br>len = input.Read(buffer, boundaryLen, 1024 - boundaryLen) + boundaryLen;<br>}<br>}<br>}  </p>
<p>return fileName;<br>}  </p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&text=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&is_video=false&description=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=在流数据中查找特定字符串&body=Check out this article: http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&title=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&name=在流数据中查找特定字符串&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2015/11/09/%E5%9C%A8%E6%B5%81%E6%95%B0%E6%8D%AE%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2/&t=在流数据中查找特定字符串" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Tmoonlight
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
